


package com.yuke.cloud.service.opc.service.impl;


import com.alibaba.fastjson.JSON;
import com.tencent.xinge.XingeApp;
import com.tencent.xinge.bean.*;
import com.tencent.xinge.push.app.PushAppRequest;
import com.yuke.cloud.service.opc.service.OpcSendNotifyService;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
@Slf4j
public class OpcSendNotifyServiceImpl implements OpcSendNotifyService {
    @Value("${android-notify.key}")
    private String androidAppId;
    @Value("${android-notify.security}")
    private String androidKey;

    @Value("${ios-notify.key}")
    private String iosAppId;
    @Value("${ios-notify.security}")
    private String iosKey;
    @Value("${ios-notify.env}")
    private String iosEnv;

    //用@Value的方式把配置文件注入
    //人物加物品发货了
    @Override
    public JSONObject sendNotifyByUser(String token, String title, String content, boolean isMessage, boolean isandriod) {
        XingeApp xingeApp = null;
        Message message = new Message();
        message.setTitle(title);
        message.setContent(JSON.toJSONString(content));
        //推送请求信息
        PushAppRequest pushAppRequest = new PushAppRequest();

        //推送平台
        if (isandriod) {
            xingeApp = new XingeApp(androidAppId, androidKey);
            pushAppRequest.setPlatform(Platform.android);
        } else {
            xingeApp = new XingeApp(iosAppId, iosKey);
            pushAppRequest.setPlatform(Platform.ios);
            MessageIOS messageIOS = new MessageIOS();
            Aps aps = new Aps();
            if (isMessage) {
                Alert alert = new Alert();
                alert.setTitle(title);
                alert.setBody(content);
//                alert.setTitle("通知");
//                alert.setBody("消息内容");
                aps.setAlert(alert);
                aps.setBadge_type(5);
            } else {
                aps.setContent_available(1);
            }
            messageIOS.setAps(aps);
            message.setIos (messageIOS);
            if (iosEnv.equals("dev")) {
                pushAppRequest.setEnvironment(Environment.dev);
            } else {
                pushAppRequest.setEnvironment(Environment.product);
            }
        }

        //消息体
        pushAppRequest.setMessage(message);
        //消息类型
        if (isMessage) {
            pushAppRequest.setMessage_type(MessageType.message);
        } else {
            pushAppRequest.setMessage_type(MessageType.notify);
        }
        //推送目标
        pushAppRequest.setAudience_type(AudienceType.token_list);

        //device_token
        ArrayList<String> token_list = new ArrayList<>();
        token_list.add(token);
        //device_token
        pushAppRequest.setToken_list(token_list);
        pushAppRequest.setPush_id("0");
//        log.info("======pushAppRequest={}", pushAppRequest);
        //推送结果 0是推送成功
        JSONObject jsonObject = xingeApp.pushApp(pushAppRequest.toString());
//        log.info("======jsonObject={}", jsonObject);
        return jsonObject;
    }


}



